﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Web.Models
{
    public class SnmpOutput
    {
        private const string vendor = "Производитель:";
        private const string model = "Модель:";
        private const string uptime = "Время работы:";
        private const string hostname = "Имя хоста:";
        public List<KeyValuePair<string, string>> dict = new List<KeyValuePair<string, string>>();

        public SnmpOutput(string data)
        {
            if (data.Contains("AT-8000S"))
            {
                // ATI AT-8000S; .1.3.6.1.4.1.207.1.4.128; 3180220600 (368.01:56:46); ; sw-uit; ; 2; 0 (00:00:00); .1.3.6.1.4.1.89.73; RS capabilities; 0 (00:00:00);
                dict.Add(new KeyValuePair<string, string>(vendor, "Allied Telesis"));
                dict.Add(new KeyValuePair<string, string>(model, "AT-8000S"));
                string patternUpTime = @"\((?<days>\d+)\.(?<H>\d{2}):(?<M>\d{2}):(?<S>\d{2})\)";
                dict.Add(new KeyValuePair<string,string>(uptime, ExtrudeUptime(data, patternUpTime)));
                Match mHostname = Regex.Match(data, patternUpTime+@"; ;\s+(?<hn>.+?);");
                if (mHostname.Success)
                {
                    dict.Add(new KeyValuePair<string,string>(hostname, mHostname.Groups["hn"].ToString()));
                }
                
            }
            if (data.Contains("Cisco NX-OS(tm) n5000"))
            {
                // Cisco NX-OS(tm) n5000, Software (n5000-uk9), Version 5.1(3)N2(1), RELEASE SOFTWARE Copyright (c) 2002-2011 by Cisco Systems, Inc. Device Manager Version 5.2(1), Compiled 3/20/2012 0:00:00; .1.3.6.1.4.1.9.12.3.1.3.1084; 266887039 (30.21:21:10.3900000); who
                dict.Add(new KeyValuePair<string,string>(vendor, "Cisco Systems"));
                dict.Add(new KeyValuePair<string,string>(model, "Nexus 5000"));
                dict.Add(new KeyValuePair<string,string>(uptime, ExtrudeUptime(data, @"\((?<days>\d+)\.(?<H>\d{2}):(?<M>\d{2}):(?<S>\d{2})\.\d+\)")));

            }
            if (data.Contains("HP J4813A ProCurve Switch 2524"))
            {
                // HP J4813A ProCurve Switch 2524, revision F.05.22, ROM F.02.01  (/sw/code/build/info(s02)); .1.3.6.1.4.1.11.2.3.7.11.19; 1017383276 (117.18:03:52.7600000); ; sw-pgs-s2; ; 74; 
                dict.Add(new KeyValuePair<string, string>(vendor, "Hewlett-Packard Company"));
                dict.Add(new KeyValuePair<string, string>(model, "HP J4813A ProCurve Switch 2524"));
                string patternUpTime = @"\((?<days>\d+)\.(?<H>\d{2}):(?<M>\d{2}):(?<S>\d{2})\.\d+\)";
                dict.Add(new KeyValuePair<string,string>(uptime, ExtrudeUptime(data, patternUpTime)));
                Match mHostname = Regex.Match(data, patternUpTime + @"; ;\s+(?<hn>.+?);");
                if (mHostname.Success)
                {
                    dict.Add(new KeyValuePair<string, string>(hostname, mHostname.Groups["hn"].ToString()));
                } 
            }
            if (data.Contains("C3560 Software"))
            {
                // data	"Cisco IOS Software, C3560 Software (C3560-IPBASE-M), Version 12.2(35)SE5, RELEASE SOFTWARE (fc1)\r\nCopyright (c) 1986-2007 by Cisco Systems, Inc.\r\nCompiled Thu 19-Jul-07 18:15 by nachen; .1.3.6.1.4.1.9.1.615; 1190930457 (137.20:08:24.5700000); ; sw-ispm; ; "

                dict.Add(new KeyValuePair<string,string>(vendor, "Cisco Systems"));
                dict.Add(new KeyValuePair<string,string>(model, "Catalyst 3560"));
                dict.Add(new KeyValuePair<string,string>(uptime, ExtrudeUptime(data, @"\((?<days>\d+)\.(?<H>\d{2}):(?<M>\d{2}):(?<S>\d{2})\.\d+\)")));
                Match mHostname = Regex.Match(data, @"\((\d+)\.(\d{2}):(\d{2}):(\d{2})\.\d+\)\W+(?<hn>.+?);");
                if (mHostname.Success)
                {
                    dict.Add(new KeyValuePair<string, string>(hostname, mHostname.Groups["hn"].ToString()));
                }
            }
        }
        private string ExtrudeUptime(string data, string patternUpTime)
        {
            Match mUptime = Regex.Match(data, patternUpTime);
            if (mUptime.Success)
            {
                return String.Format("{0}д. {1}:{2}:{3}",
                                    mUptime.Groups["days"].ToString(),
                                    mUptime.Groups["H"].ToString(),
                                    mUptime.Groups["M"].ToString(),
                                    mUptime.Groups["S"].ToString());
            }
            else
            {
                return "Неопределено.";
            }
        }
    }
}